import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from "@/views/Index";
import Other from "@/views/Other";
import Mine from "@/views/Mine";
import Video from "@/views/Video";
// import store from "@/store/index"
import {getUserInfo} from "@/api/api";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Home',
        component: Index
    },
    {
        path: '/index',
        name: 'Home',
        component: Index
    },
    {
        path: '/other',
        name: "other",
        component: Other
    },
    {
        path: '/detail/:id',
        name: "detail",
        component: Other
    },
    {
        path: '/mine',
        name: 'mine',
        component: Mine,
        meta: {
            needLogin: true,
            number: 1
        },
    },
    {
        path: '/video',
        name: "video",
        component: Video,
        meta: {
            needLogin: true
        }
    }


]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {
    if (to.meta.needLogin) {
        getUserInfo().then(res=>{
            if(res.code == 0){
                next()
            }else{
                next("/")
                // store.commit("changeShowLogin", true)
            }
        })
    } else {
        next()
    }
})

export default router
